const fsExtra = require('fs-extra')
const path = require('path')
const fs = require('fs')
const { promisify } = require('util')
const fsWriteFile = promisify(fs.writeFile)
const logger = require('./logger')
const upperCaseFirst = str => str.charAt(0).toUpperCase() + str.slice(1)
const slashToCamel = str => str.replace(/\-(\w)/g, (all, letter) => letter.toUpperCase())
const isJson = target => {
  const ext = path.extname(target)
  return ext ? ext.includes('.json') : false
}
const genDir = async path => {
  return await fsExtra.mkdirp(path)
}
const genFile = async (name, targetPath, payload) => {
  const combineFile = path.resolve(targetPath, name)
  return isJson(combineFile)
    ? await fsExtra.writeJSON(combineFile, payload, { spaces: 2 })
    : await fsWriteFile(combineFile, payload)
}

module.exports = {
  logger,
  upperCaseFirst,
  genDir,
  genFile,
  slashToCamel,
}
